package top.heue.text.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import top.heue.text.bean.ContentBean;
import top.heue.text.bean.ContentListBean;
import top.heue.text.bean.MainListBean;
import top.heue.text.util.RunUtil;

public class SQLite {
    public final String
            TABLE = "text",
            ID = "id",
            TITLE = "title",
            CONTENT = "content";
    private final Context context;
    SQLiteDatabase database;

    public SQLite(Context context) {
        database = context.openOrCreateDatabase("content.db", Context.MODE_PRIVATE, null);
        this.context = context;
    }

    public long insert(String title, String content) {
        ContentValues values = new ContentValues();
        values.put(TITLE, title);
        values.put(CONTENT, content);
        long r = database.insert(TABLE, "id", values);
        close();
        return  r;
    }

    public void delete(String id) {
        database.delete(TABLE, "id=?", new String[]{id});
        close();
    }

    public ContentBean query(String id) {
        ContentBean contentBean = new ContentBean("-1", "载入失败", "打不开，怎么办QAQ");
        try (Cursor cursor = database.rawQuery("select * from " + TABLE + " where id='" + id + "'", null)) {
            if (cursor.moveToNext()) {
                //String title = cursor.getString(cursor.getColumnIndex(TITLE));
                contentBean = new ContentBean(
                        cursor.getString(cursor.getColumnIndex(ID)),
                        cursor.getString(cursor.getColumnIndex(TITLE)),
                        cursor.getString(cursor.getColumnIndex(CONTENT))
                );
            }
        } finally {
            close();
        }
        return contentBean;
    }

    public List<MainListBean> listAll() {
        List<MainListBean> list = new ArrayList<>();
        MainListBean mainListBean;
        try (Cursor cursor = database.rawQuery("select * from " + TABLE, null)) {
            while (cursor.moveToNext()) {
                String content = cursor.getString(cursor.getColumnIndex(CONTENT));
                content = RunUtil.contentFormat(context, content);
                mainListBean = new MainListBean(
                        cursor.getString(cursor.getColumnIndex(ID)),
                        cursor.getString(cursor.getColumnIndex(TITLE)),
                        content
                );
                list.add(0, mainListBean);
                //for (int i = 0; i < cursor.getColumnCount(); i++);
            }
       } finally {
            close();
        }
        return list;
    }

    public boolean isTableExits() {
        String sql = "select count(*) as c from sqlite_master where type ='table' and name='" + TABLE + "';";
        int count;
        try (Cursor cursor = database.rawQuery(sql, null)) {
            count = 0;
            if (cursor.moveToNext()) {
                count = cursor.getInt(0);
            }
        }
        close();
        return count > 0;
    }

    public void createTable() {
        String sql = "create table " + TABLE +
                " (id INTEGER PRIMARY KEY AUTOINCREMENT," +
                " title TEXT NOT NULL," +
                " content TEXT NOT NULL)";
        database.execSQL(sql);
        close();
    }

    public void close() {
        database.close();
    }
}
